# -*- coding: utf-8 -*-

import unittest
import os.path

from players_order import PlayersOrderWidget, rgb
from presenter_mock import PresenterMock
from model.gamestate import *

NUM_PLAYERS = 10
NUM_REPETITIONS = 50


def get_players_names_from_gamestate(gamestate):
    return [player.name for player in gamestate.players.players]


class PlayersOrderTest(unittest.TestCase):
    def setUp(self):
        self.names = [str(i) for i in range(NUM_PLAYERS)]
        players = Players([Player(name, []) for name in self.names])
        players.set_next_pair_strategy(NextPairStrategy(players.players))
        hat = Hat(['стул', 'шапка', 'кастрюлька'])
        game_round = GameRound(20, [], '', (
            players.players[0], players.players[2]))

        self.gamestate = GameState(players, hat, game_round,
                                   0, [], None, Settings())
        self.presenter = PresenterMock(Phase.FROM_TO)
        self.widget = PlayersOrderWidget(self.gamestate, self.presenter)

    def test_shuffle(self):
        for i in range(NUM_REPETITIONS):
            self.widget.shuffle()
            self.assertEquals(self.presenter.is_phase_changed, False)
            self.assertEquals(
                set(self.names),
                set(get_players_names_from_gamestate(self.widget.gamestate)))

        self.widget.finish()
        self.assertEquals(self.presenter.is_phase_changed, True)

    def test_rgb(self):
        self.assertEquals(rgb('000000'), [0, 0, 0, 1])
        self.assertEquals(rgb('FFFFFF'), [1, 1, 1, 1])
        self.assertEquals(rgb('FF0000'), [1, 0, 0, 1])
        self.assertEquals(rgb('00FF00'), [0, 1, 0, 1])
        self.assertEquals(rgb('0000FF'), [0, 0, 1, 1])


if __name__ == '__main__':
    import gettext
    LOCALE_PATH = '/home/ivan/thehat/locales/'
    gettext.bindtextdomain('thehat', LOCALE_PATH)
    lang = gettext.translation('thehat', LOCALE_PATH, ['ru_RU'],
                               codeset='utf-8')
    lang.install(True)

    from kivy.lang import Builder
    Builder.load_file(os.path.join(os.path.dirname(__file__),
                                   'players_order.kv'))

    unittest.main()
